#pragma once
#include "Sprite.h"
#include "Logger.h"
#include "Tile.h"
namespace Pulpy
{
	
	class TileSystem
	{
	private:
		Sprite * _Sprite;
		Tile ** _TileList;
		int _Height;
		int _Width;
		int _Rows;
		int _Cols;
	public:
		TileSystem(int tileWidth, int tileHeight): _Width(tileWidth), _Height(tileHeight), _Rows(0), _Cols(0),_Sprite(NULL),_TileList(NULL) { }
		~TileSystem() { 
			for(int i = 0; i < _Rows; ++i)
				delete [] _TileList[i];
			delete [] _TileList; 
		}
		void setSprite(char * filename, int row, int col) { _Sprite = new Sprite(col,row,filename); }
		void readTiles(char * filename);
		void saveTiles(char * filename);
		void setTile(int row, int col, unsigned short type) { _TileList[row][col].setType(type); if(type == 0) _TileList[row][col].setSolid(false); }
		void newTileSet(int row, int col)
		{
			for(int i = 0; i < _Rows; ++i)
				delete [] _TileList[i];
			delete [] _TileList; 
			_Rows = row;
			_Cols = col;
			_TileList = new Tile*[_Rows];
			for(int i = 0; i < _Rows; ++i)
				_TileList[i] = new Tile[_Cols];
		}
		void draw();
		int collideWith(Object2D & ob);
		int collideBelow(Object2D & ob, Tile & t);
		int collideRight(Object2D & ob, Tile & t);
		int collideLeft(Object2D & ob, Tile & t);
		int collideTop(Object2D & ob, Tile & t);
	};
}